package com.stock.demo.function.start.presenter;

import android.text.TextUtils;

import com.stock.demo.function.start.view.RegisterActivity;
import com.stock.demo.function.start.view.RegisterView;
import com.stock.demo.util.CacheUtils;
import com.stock.demo.util.CommonUtils;
import com.stock.demo.util.Constants;

/**
 * Created by XuZhaoyu on 2017/6/25.
 */

public class RegisterPresenterImpl implements RegisterPresenter {

    public RegisterView mRegisterView;

    public RegisterPresenterImpl(RegisterActivity registerActivity) {
        mRegisterView = registerActivity;
    }

    @Override
    public void getCode(String phone) {
        mRegisterView.onGetCode(true);
    }

    @Override
    public void register(String username, String password) {
        if (mRegisterView instanceof RegisterActivity) {
            CacheUtils.get().put(Constants.KEY_USERNAME, username);
            CacheUtils.get().put(Constants.KEY_PASSWORD_MD5, CommonUtils.md5(password));
            mRegisterView.onRegister(true);
        }
    }

    @Override
    public void login(String username, String password) {
        if (mRegisterView instanceof RegisterActivity) {
            if (TextUtils.equals(CacheUtils.get(((RegisterActivity) mRegisterView)).getAsString(Constants.KEY_USERNAME), username) && TextUtils.equals(CacheUtils.get(((RegisterActivity) mRegisterView)).getAsString(Constants.KEY_PASSWORD_MD5), CommonUtils.md5(password))) {
                mRegisterView.onLogin(true);
            } else {
                mRegisterView.onLogin(false);
            }
        }
    }
}
